<template>
  <div>
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>分类参数</el-breadcrumb-item>
    </el-breadcrumb>

    <el-card>
      <el-alert
        title="注意：只允许为第三级分类设置相关参数"
        type="warning"
        show-icon
        :closable="false"
      >
      </el-alert>
      选择商品分类：
      <!-- {{ value }} -->
      <el-cascader
        @change="handleClick"
        :options="options"
        v-model="value"
        :props="props"
        clearable
      >
      </el-cascader>
      <el-tabs v-model="activeName" @tab-click="handleClick">
        <el-tab-pane label="动态参数" name="many">
          <el-button type="primary" @click="add" :disabled="dis"
            >添加参数</el-button
          >
          <el-table
            :data="arr"
            style="width: 100%"
            border
            :header-cell-style="{ background: '#EEF1F6', color: '#606266' }"
          >
            <el-table-column type="expand">
              <template slot-scope="scope">
                <!-- {{ scope.row.attr_vals }} -->
                <el-tag
                  :key="tag.attr_id"
                  v-for="(tag, index) in scope.row.attr_vals"
                  closable
                  :disable-transitions="false"
                  @close="handleClose(scope.row, index)"
                >
                  {{ tag }}
                </el-tag>
                <el-input
                  class="input-new-tag"
                  v-if="scope.row.inputVisible"
                  v-model="scope.row.inputValue"
                  ref="saveTagInput"
                  size="small"
                  @keyup.enter.native="handleInputConfirm(scope.row)"
                  @blur="handleInputConfirm(scope.row)"
                >
                </el-input>
                <el-button
                  v-else
                  class="button-new-tag"
                  size="small"
                  @click="showInput(scope.row)"
                  >+ New Tag</el-button
                >
              </template>
            </el-table-column>
            <el-table-column label="#" type="index" width="50">
            </el-table-column>
            <el-table-column prop="attr_name" label="参数名称">
            </el-table-column>
            <el-table-column label="操作">
              <template slot-scope="scope">
                <el-button
                  size="mini"
                  type="primary"
                  icon="el-icon-edit"
                  @click="update(scope.row)"
                  >编辑</el-button
                >
                <el-button
                  size="mini"
                  type="danger"
                  icon="el-icon-delete"
                  @click="del(scope.row)"
                  >删除</el-button
                >
              </template>
            </el-table-column>
          </el-table>
        </el-tab-pane>

        <el-tab-pane label="静态属性" name="only">
          <el-button type="primary" @click="add" :disabled="dis"
            >添加参数</el-button
          >
          <el-table
            :data="arr"
            :header-cell-style="{ background: '#EEF1F6', color: '#606266' }"
          >
            <el-table-column type="expand">
              <template slot-scope="scope">
                <!-- {{ scope.row.attr_vals }} -->
                <el-tag
                 :key="tag.attr_id"
                  v-for="(tag, index) in scope.row.attr_vals"
                  closable
                  :disable-transitions="false"
                  @close="handleClose(scope.row,index)"
                >
                  {{ tag }}
                </el-tag>
                <el-input
                  class="input-new-tag"
                  v-if="scope.row.inputVisible"
                  v-model="scope.row.inputValue"
                  ref="saveTagInput"
                  size="small"
                  @keyup.enter.native="handleInputConfirm(scope.row)"
                  @blur="handleInputConfirm(scope.row)"
                >
                </el-input>
                <el-button
                  v-else
                  class="button-new-tag"
                  size="small"
                  @click="showInput(scope.row)"
                  >+ New Tag</el-button
                >
              </template>
            </el-table-column>
            <el-table-column label="#" type="index" width="50">
            </el-table-column>
            <el-table-column prop="attr_name" label="属性名称">
            </el-table-column>
            <el-table-column label="操作">
              <template slot-scope="scope">
                <el-button
                  size="mini"
                  type="primary"
                  icon="el-icon-edit"
                  @click="update(scope.row)"
                  >编辑</el-button
                >
                <el-button
                  size="mini"
                  type="danger"
                  icon="el-icon-delete"
                  @click="del(scope.row)"
                  >删除</el-button
                >
              </template>
            </el-table-column>
          </el-table>
        </el-tab-pane>
      </el-tabs>
    </el-card>

    <el-dialog :title="total" :visible.sync="dialogVisible" width="40%">
      <el-form label-position="left" label-width="80px">
        <el-form-item label="参数名称">
          <el-input size="medium" v-model="form.attr_name"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="En">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      value: [],
      options: [],
      dialogVisible: false,
      arr: [],
      dis: true,
      form:{
        attr_name: ""
      },
      title: "",
      coll: false,
      activeName: "many",
      props: {
        children: "children",
        value: "cat_id",
        label: "cat_name",
      },
      attr_id: 0,
    };
  },
  methods: {
    handleClose(row, index) {
      // this.dynamicTags.splice(this.dynamicTags.indexOf(tag), 1);
        row.attr_vals.splice(index, 1);
        this.aa(row);
    },

    showInput(row) {
      row.inputVisible = true;
      this.$nextTick((_) => {
        this.$refs.saveTagInput.focus();
      });
    },

    handleInputConfirm(row) {
      let inputValue = row.inputValue;
      if (inputValue) {
        row.attr_vals.push(inputValue);
      }
      row.inputVisible = false;
      row.inputValue = "";
      this.aa(row);
    },
    //删除或添加参数指定的 属性值
    async aa(row) {
      // console.log(row, 1);
        var { data: res } = await this.$http.put(
          `categories/${this.value[2]}/attributes/${row.attr_id}`,
          {
            attr_name: row.attr_name,
            attr_sel: row.attr_sel,
            attr_vals: row.attr_vals.join(","),
          }
        );
        // console.log(res);
    },
    //删除按钮
    del(row) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          var res = await this.$http.delete(
            `categories/${this.value[2]}/attributes/${row.attr_id}`
          );
          this.hint(res);
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    //编辑按钮
    update(row) {
      this.dialogVisible = true;
      this.coll = false;
      Object.assign(this.form,row)
      this.attr_id = row.attr_id;
    },
    //添加按钮
    add() {
      this.dialogVisible = true;
      this.coll = true;
      this.attr_name = "";
    },
    //添加 删除 对话框确定按钮
    async En() {
      this.dialogVisible = false;

      if (this.coll == true) {
        //添加动态参数 添加静态属性
        var res = await this.$http.post(
          `categories/${this.value[2]}/attributes`,
          {
            attr_name: this.attr_name,
            attr_sel: this.activeName,
          }
        );
        this.hint(res);
      } else {
        //修改动态参数 修改静态属性
        var res = await this.$http.put(
          `categories/${this.value[2]}/attributes/${this.attr_id}`,
          {
            attr_name: this.form.attr_name,
            attr_sel: this.activeName,
            attr_vals:this.form.attr_vals.join(',')
          }
        );
        this.hint(res);
      }
    },
    //tabs 标签页 切换事件
    async handleClick() {
      //添加按钮 是否 能够使用
      if (this.value == "") {
        this.dis = true;
        this.$message({
          showClose: true,
          message: "请选择第三级分类",
        });
        this.arr = [];
      } else {
        this.dis = false;
        var { data: res } = await this.$http.get(
          `categories/${this.value[2]}/attributes`,
          {
            params: {
              sel: this.activeName,
            },
          }
        )
          res.data.forEach((item) => {
            item.attr_vals = item.attr_vals ? item.attr_vals.split(",") : [],
              item.inputVisible = false,
              item.inputValue = ""
          });
          // console.log(res.data);
          this.arr = res.data;
        
      }
    },
    //成功 失败 提示信息
    hint(res) {
      if (res.data.meta.status == 200 || res.data.meta.status == 201) {
        this.$message({
          showClose: true,
          message: res.data.meta.msg,
          type: "success",
        });
        this.handleClick();
      } else {
        this.$message({
          showClose: true,
          message: res.data.meta.msg,
          type: "error",
        });
      }
    },
    async getData() {
      var { data: res } = await this.$http.get("categories");
      this.options = res.data;
    },
  },
  computed: {
    total() {
      if (this.coll == true && this.activeName == "many") {
        return (this.title = "添加动态参数");
      } else if (this.coll == true && this.activeName == "only") {
        return (this.title = "添加静态属性");
      } else if (this.coll == false && this.activeName == "many") {
        return (this.title = "修改动态参数");
      } else {
        return (this.title = "修改静态属性");
      }
    },
  },
  created() {
    this.getData();
  },
};
</script>
<style scoped>
.el-tag {
  margin-left: 10px;
}
.input-new-tag {
  width: 90px;
  margin-left: 10px;
}

.el-cascader {
  width: 230px !important;
}
</style>